package org.bringer.dao.hibernate;

import java.util.Calendar;
import java.util.List;

import org.bringer.dao.AnnotationDao;
import org.bringer.dao.SearchException;
import org.bringer.model.Annotation;
import org.hibernate.Query;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

/**
 * This class interacts with Hibernate session to save/delete and
 * retrieve User objects.
 *
 * @author <a href="mailto:matt@raibledesigns.com">Matt Raible</a>
 *   Modified by <a href="mailto:dan@getrolling.com">Dan Kibler</a>
 *   Extended to implement Acegi UserDetailsService interface by David Carter david@carter.net
 *   Modified by <a href="mailto:bwnoll@gmail.com">Bryan Noll</a> to work with
 *   the new BaseDaoHibernate implementation that uses generics.
 *   Modified by jgarcia (updated to hibernate 4)
*/
@Repository
public class AnnotationDaoHibernate extends GenericDaoHibernate<Annotation, Long> implements AnnotationDao {

    /**
     * Constructor that sets the entity to User.class.
     */
    public AnnotationDaoHibernate() {
        super(Annotation.class);
    }

	@Override
	public List<Annotation> getAnnotationByDescription(String description) {
		List<Annotation> accounts = getSession().createCriteria(Annotation.class).add(Restrictions.like("description", description,MatchMode.ANYWHERE)).list();
        if (accounts == null || accounts.isEmpty()) {
            throw new SearchException("Annotations not founded with that criteria (description):"+description);
        } else {
            return accounts;
        }		
	}

	@Override
	public List<Annotation> getAnnotationsByAccount(String iban) {
		 Query qry = getSession().createQuery("from Annotation A where A.account.ibanCode=:ibanCode");
		 qry.setParameter("ibanCode", iban);
		 List<Annotation> accounts=qry.list();
		 if (accounts == null || accounts.isEmpty()) {
	            throw new SearchException("Annotations not founded with that criteria (ibanCode):"+iban);
	        } else {
	            return  accounts;
	        }		
	}

	@Override
	public List<Annotation> getAnnotationsByAccountInDates(String iban,
			Calendar initialDate, Calendar endDate) {
		// TODO Auto-generated method stub
		 Query qry = getSession().createQuery("from Annotation A where  A.account.ibanCode=:ibanCode AND A.annotationDate >= :iniDate AND A.annotationDate <= :endDate");
		 qry.setParameter("ibanCode", iban);
		 qry.setParameter("iniDate", initialDate);
		 qry.setParameter("endDate", endDate);
		 
		 List<Annotation> accounts=qry.list();
		 if (accounts == null || accounts.isEmpty()) {
	            throw new SearchException("Annotations not founded with that criteria (ibanCode):"+iban +"in range:"+initialDate.getTime()+" "+endDate.getTime());
	        } else {
	            return  accounts;
	        }		
	}

	@Override
	public List<Annotation> getAnnotationsByAccountAboveX(String iban,
			Double amount) {
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub
		 Query qry = getSession().createQuery("from Annotation A where  A.account.ibanCode=:ibanCode AND A.amount>:amount");
		 qry.setParameter("ibanCode", iban);
		 qry.setParameter("amount", amount);		
		 
		 List<Annotation> accounts=qry.list();
		 if (accounts == null || accounts.isEmpty()) {
	            throw new SearchException("Annotations not founded with that criteria (ibanCode):"+iban +"below amount:"+amount);
	        } else {
	            return  accounts;
	        }		
	}
	
/**
	@Override
	public Account getAccountByIban(String iban) {
		// TODO Auto-generated method stub
		List<Account> accounts = getSession().createCriteria(Account.class).add(Restrictions.eq("ibanCode", iban)).list();
        if (accounts == null || accounts.isEmpty()) {
            throw new SearchException("Accounts not founded with that criteria:"+iban);
        } else {
            return (Account) accounts.get(0);
        }		
	}

	@Override
	public List<Account> getAccountsByUser(User user) {
		 Query qry = getSession().createQuery("from Account where owner_id=:o_id");
		 qry.setParameter("o_id", user.getId());
		 List<Account> accounts=qry.list();
		 if (accounts == null || accounts.isEmpty()) {
	            throw new SearchException("Accounts not founded with that criteria (user):"+user.getId());
	        } else {
	            return  accounts;
	        }		
	}

	@Override
	public List<Account> getAccountsByDescription(String desc) {
		List<Account> accounts = getSession().createCriteria(Account.class).add(Restrictions.like("description", desc, MatchMode.ANYWHERE)).list();
        if (accounts == null || accounts.isEmpty()) {
            throw new SearchException("Accounts not founded with that criteria:"+desc);
        } else {
            return accounts;
        }		
	}
**/
}
